<?php 
class Cronjob extends CI_Controller {
	public function __construct() {
		parent::__construct();
		
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->model('Jobs');
		$this->load->model('Company');
		$this->load->model('Rinvoices');
	}
	
	public function index() {
		redirect('/');
	}
        
        public function remindoverdue()
        {
            $adminEmail = 'ivy@bookabus.sg';
            $adminName = 'Ivy';
            $bccEmail = 'phamhphuc.bk@gmail.com';
//            $adminEmail = 'phamhphuc.bk@gmail.com';
//            $adminName = 'Phuc';
            $this->load->library('email');
			
			// intial email
			$mailconf['mailtype'] = 'html';
			$this->email = new CI_Email($mailconf);
			
            $overdue = $this->Jobs->getOverduePriceList(-1);
//            echo "<pre>"; print_r($overdue);die;             
            $html = '<table width="800" cellpadding="5" border=1 cellspacing="0">
                        <tr>    
                            <th>Job ID</th>
                            <th>Details</th>
                            <th>Due date</th>
                            <th>Total</th>
                            <th>Paid</th>
                            <th>Left to pay</th>                            
                        </tr>
            ';
            // generate HTML
            foreach ($overdue as $jobOrder){
                    $job = $jobOrder['order'];
                    $routes = $jobOrder['routes'];
                    $insideString = "";
                    foreach ($routes as $route){
                        $insideString .= nl2br($this->Jobs->formatJobInfo($route));
                        }
                    $html .= '                   
                            <tr>
                                    <td valign="top">' . $this->Jobs->getID($job) . '</td>
                                    <td>
                                            ' . $insideString . '
                                            </td>
                                    <td valign="top"> ' . date('d M Y', strtotime($job->duedate)) . '</td>
                                    <td valign="top"> ' . $this->Jobs->formatPrice($job->price) . '</td>
                                    <td valign="top"> ' . $this->Jobs->formatPrice($job->paid) . '</td>
                                    <td valign="top"> ' . $this->Jobs->formatPrice($job->price-$job->paid) . '</td>                                                                        
                            </tr>'
                    ;
            }
            $html .= '</table>';
//            echo $html;die;
//            echo "<pre>"; print_r($overdue);die;                                                
            
            $this->email->from("no-reply@bookabus.sg", $adminName);
            $this->email->to($adminEmail);
            $this->email->bcc($bccEmail);
            $this->email->subject('Bookabus.sg – Overdue jobs remind!!!');
            $this->email->message('Hello, '.$adminName.',
                    <br /><br />
                    Overdue jobs on Bookabus.sg until <b>' . date("Y-m-d H:i") . '</b>:
                    <br /><br />
                    '.$html.'
                    <br /><br />
                    Thank you.
                    <br /><br />
                    Best Regards,
                    <br />Ivy');
            $this->email->send();
            
			/*____send to developer______________________*/
			$this->email->subject('Bookabus.sg - Developer Test - cronjob.php - line 84');
			$this->email->to('hoang.dinh21@gmail.com');
			$this->email->send();
			/*__________________________________________*/
					
            echo "Sent Notification to Admin<br/>";
            
            // send email to each user
            foreach($overdue as $jobOrder) {
                $html = '<table width="800" cellpadding="5" border=1 cellspacing="0">
                            <tr>    
                                <th>Job ID</th>
                                <th>Details</th>
                                <th>Due date</th>
                                <th>Total</th>
                                <th>Paid</th>
                                <th>Left to pay</th>                            
                            </tr>
                ';
                $job = $jobOrder['order'];
                $routes = $jobOrder['routes'];
                $userinfo = $jobOrder['userinfo'];
                $insideString = "";
                foreach ($routes as $route){
                    $insideString .= nl2br($this->Jobs->formatJobInfo($route));
                }
                $html .= '                   
                        <tr>
                                <td valign="top">' . $this->Jobs->getID($job) . '</td>
                                <td>
                                        ' . $insideString . '
                                        </td>
                                <td valign="top"> ' . date('d M Y', strtotime($job->duedate)) . '</td>
                                <td valign="top"> ' . $this->Jobs->formatPrice($job->price) . '</td>
                                <td valign="top"> ' . $this->Jobs->formatPrice($job->paid) . '</td>
                                <td valign="top"> ' . $this->Jobs->formatPrice($job->price-$job->paid) . '</td>                                                                        
                        </tr>'
                ;
                $html .= '</table>';
                
                // sample data
                $email = $userinfo->email;
                $name = $userinfo->name;

                // send to user
                $this->email->from($adminEmail, $adminName);
                $this->email->to($email);
                $this->email->bcc($bccEmail);
                $this->email->subject('Bookabus.sg – Overdue jobs remind [' . $name . ']!!!');
                $this->email->message('Hello, '.$name.',
                        <br /><br />
                        Your overdue jobs on Bookabus.sg until <b>' . date("Y-m-d H:i") . '</b> are:
                        <br /><br />
                        '.$html.'
                        <br /><br />
                        Thank you.
                        <br /><br />
                        Best Regards,
                        <br />Ivy');
                $this->email->send();
				
				/*____send to developer______________________*/
				$this->email->subject('Bookabus.sg - Developer Test - cronjob.php - line 146');
				$this->email->to('hoang.dinh21@gmail.com');
				$this->email->send();
				/*__________________________________________*/
                echo "Sent Notification to {$name}[{$email}]<br/>";
            }
            die;
        }
}
?>